// JavaScript Document
function goAjax()
{
var req = false;
try
  {
    req = new XMLHttpRequest(); /* e.g. Firefox */
  }
catch(err1)
  {
  try
    {
     req = new ActiveXObject("Msxml2.XMLHTTP");
  /* some versions IE */
    }
  catch(err2)
    {
    try
      {
       req = new ActiveXObject("Microsoft.XMLHTTP");
  /* some versions IE */
      }
      catch(err3)
        {
         req = false;
        }
    }
  }
return req;
}

var I;

function inchide(){
	var divul = document.getElementById("mesajul");
	divul.style.display = "none";
	clearInterval(I);
}

function arata_mesaj(str,culoare){
	clearInterval(I);
	I = setInterval(inchide,4000);
	
	var divul = document.getElementById("mesajul");
	divul.innerHTML = str;
	divul.style.background = culoare;
	divul.style.fontWeight = "bold";
	divul.style.color = "#cc0000";
	divul.style.border = "#ffffff 1px solid";
	divul.style.display = "block";
}

function isEmail(str) {

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
  
   return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	 return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
   
	return false
}

 if (str.indexOf(at,(lat+1))!=-1){
   
	return false
 }

 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
   
	return false
 }

 if (str.indexOf(dot,(lat+2))==-1){
   
	return false
 }

 if (str.indexOf(" ")!=-1){
   
	return false
 }

 return true					
}

var userChecked = false;
var emailChecked = false;

var limba;

function validare_register(f,$CS,lang){
	limba = lang;
	var required = Array("username","password","password2","email","codul");
	for(var i=0;i<required.length;i++){
		if(f.elements[required[i]].value == ""){
			var s = document.getElementById(required[i]+"_s");
			s.style.color = "#cc0000";
			s.style.fontWeight = "bold";
			s.innerHTML = "&laquo; "+((lang== "ro")?"Camp obligatoriu":"Required Field!");
			f.elements[required[i]].focus();
			return false;
		}
	}
	if(f.password.value != f.password2.value){
		var s = document.getElementById("password2_s");
		s.style.color = "#cc0000";
		s.style.fontWeight = "bold";
		s.innerHTML = "&laquo; "+((lang == "ro")?"Cele doua parola nu coincid":"Wrong Password!");
		return false;
	}
	if(f.password.value.length < 6){
		var s = document.getElementById("password2_s");
		s.style.color = "#cc0000";
		s.style.fontWeight = "bold";
		s.innerHTML = "&laquo; "+((lang == "ro")?"Parola trebuie sa aibe minimum 6 caractere!":"The password must be at least 6 chars long!");
		return false;
	}
	if(!isEmail(f.email.value)){
		var s = document.getElementById("email_s");
		s.style.color = "#cc0000";
		s.style.fontWeight = "bold";
		s.innerHTML = "&laquo; "+((lang == "ro")?"Email invalid":"Invalid Email Address!");
		return false;
	}
	if(f.zi.value == 0 || f.luna.value == 0 || f.an.value == 0){
		var s = document.getElementById("data_n_s");
		s.style.color = "#cc0000";
		s.style.fontWeight = "bold";
		s.innerHTML = "&laquo; "+((lang == "ro")?"Alegeti data nasterii":"Please choose your birth date!");
		return false;
	}
	if(!f.termeni.checked){
		alert((lang == "ro")?"Pentru a putea trece mai departe, va rugam sa cititi termenii si conditiile de utilizare ale siteului.\nVa multumim!":"To move on, please read the terms and conditions of use of the site.\nThank you!");
		return false;
	}
	if(!userChecked){
		alert((lang == "ro")?"Numele de utilizator ales nu este valid!\nTrebuie sa aibe minimum 4 caractere si sa contina numai caractere alfa-numerice, sau oricare dintre caracterele _ - .":"The username you chose is not valid!\nIt must have minimum 4 characters and contain only alpha-numeric characters or - _ .");
		return false;
	}
	if(!emailChecked){
		alert((lang == "ro")?"Adresa de email nu este valida!":"The email address it`s not correct!");
		return false;
	}
	if(f.pt_spam.value != f.codul.value){
		var s = document.getElementById("codul_s");
		s.style.color = "#cc0000";
		s.style.fontWeight = "bold";
		s.innerHTML = "&laquo; "+((lang == "ro")?"Introduceti codul anti-spam!":"Please insert the anti-spam code!");
		return false;
	}else{
		f.action = $CS+"prelucrare_date.php";
	}
}

function hide_them(f){
	var required = Array("username","password","password2","email","data_n","codul");
	for(var i=0;i<required.length;i++){
		var s = document.getElementById(required[i]+"_s");
		s.innerHTML = "";
	}
}
function set_spam(val){
	document.getElementById("pt_spam").value = val;
	var trim = goAjax();
	var url = "set_anti_spam.php?codul="+val;
	trim.open("GET",url);
	trim.onreadystatechange = setat;
	trim.send(null);
}
function setat(){
	if(this.readyState == 4){
		//alert(this.responseText);
	}
}
var innH="";

function verifica_valabilitate(username){
	var divul = document.getElementById('check_user');
		
	if(innH == ""){
		innH = divul.innerHTML;
	}
	
	if(username.length > 3){
		
		//document.inregistrare.username.disabled = true;
	
		divul.style.display = 'block';
		
		divul.innerHTML = innH;
		
		divul.className = '';
		
		var check = goAjax();
		var url = 'checkUsername.php?username='+username;
		check.open('GET',url);
		check.onreadystatechange = verificare_gata;
		check.send(null);
	}else{
		userChecked = false;
		divul.style.display = 'none';
		divul.innerHTML = innH;
	}
}
function verificare_email(email){ 
	var divul = document.getElementById('check_email');
		
	if(innH == ""){
		innH = divul.innerHTML;
	}
	if(email.length > 3 && isEmail(email)){
		
		//document.inregistrare.username.disabled = true;
	
		divul.style.display = 'block';
		
		divul.innerHTML = innH;
		
		divul.className = '';
		
		var check = goAjax();
		var url = 'checkEmail.php?email='+email;
		check.open('GET',url);
		check.onreadystatechange = verificare_gata_email;
		check.send(null);
	}else{
		emailChecked = false;
		divul.style.display = 'none';
		divul.innerHTML = innH;
	}
}

function verificare_gata(){
	if(this.readyState == 4){
		document.inregistrare.username.disabled = false;
		var divul = document.getElementById('check_user');
		divul.innerHTML = "";
		if(this.responseText == "ok"){
			userChecked = true;
			divul.className = "check_ok";
			if(limba == 'ro'){
				divul.innerHTML = 'Acest nume de utilizator este liber si il puteti folosi!';
			}else{
				divul.innerHTML = 'Congrats! This username it`s available!';
			}
		}else{
			userChecked = false;
			divul.className = "check_not_ok";
			if(limba == 'ro'){
				divul.innerHTML = 'Ne pare rau dar acest nume de utilizator nu este liber.';
			}else{
				divul.innerHTML = 'Sorry! This username it`s not available!';
			}
		}
	}
}

///////////////////
function verificare_gata_email(){
	if(this.readyState == 4){
		document.inregistrare.username.disabled = false;
		var divul = document.getElementById('check_email');
		divul.innerHTML = "";
		if(this.responseText == "ok"){
			emailChecked = true;
			divul.className = "check_ok";
			if(limba == 'ro'){
				divul.innerHTML = 'Adresa de email este valida!';
			}else{
				divul.innerHTML = 'Congrats! This email address it`s available!';
			}
		}else{
			emailChecked = false;
			divul.className = "check_not_ok";
			if(limba == 'ro'){
				divul.innerHTML = 'Ne pare rau dar aceatsa adresa este folosita pentru un alt cont.';
			}else{
				divul.innerHTML = 'We`re sorry! This email address it`s already used!';
			}
		}
	}
}


//////////////////
function valid_contact(f){
	if(f.name.value == "" || f.email.value == "" || f.message.value == ""){
		alert("All fields are required.");
		return false;
	}
	if(!isEmail(f.email.value)){
		alert("The email address it`s not valid! Please correct this!");
		return false;
	}
	return true;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
var apasat = false;
function onmover(s){
if(!apasat){
var id = s.id.substr(1);
for(var i=0;i<=id;i++){
var stea = document.getElementById("s"+i);
stea.src = 'images/stea_on.jpg';
}
}
}
function onmout(s){
if(!apasat){
var id = s.id.substr(1);
for(var i=0;i<=id;i++){
var stea = document.getElementById("s"+i);
stea.src = 'images/stea_off.jpg';
}
}
}
function onmclick(s,aa){
if(!apasat){
apasat = true;
var id = s.id.substr(1);

var vot = goAjax();
var url = "voteaza.php?nota="+(parseInt(id)+1)+"&articol_id="+aa;
vot.open("GET",url);
vot.onreadystatechange = function(){
if(this.readyState == 4){
document.getElementById('nota_vot').innerHTML = this.responseText;
}
}
vot.send(null);

}
}

function validarea3(f,require){
	a = require.split(",");
	for(i=0;i<a.length;i++)
	if(f.elements[a[i]].value=="" && (f.elements[a[i]].type!="checkbox" && f.elements[a[i]].type!="submit")){
		alert("Va rugam sa completati campurile marcate cu steluta.");
		f.elements[a[i]].focus();
		return false;
	}
	if(!isEmail(f.email.value)){
		alert("Va rugam sa introduceti o adresa de email valida!");
		return false;
	}
}